package com.xiaoer.studyenglishdemo.ui;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.xiaoer.studyenglishdemo.R;
import com.xiaoer.studyenglishdemo.bean.WordBean;

import java.util.ArrayList;
import java.util.List;

public class WordsDetailActivity extends AppCompatActivity {

    ImageView iv_back;
    ImageView iv_img;
    LinearLayout ll_btn1;
    TextView tv_introduce;
    LinearLayout ll_btn2;
    TextView tv_means;
    LinearLayout ll_btn3;
    TextView tv_short_sentence;
    LinearLayout ll_btn4;
    TextView tv_sentence1;
    LinearLayout ll_btn5;
    TextView tv_sentence2;
    LinearLayout ll_btn6;
    TextView tv_sentence3;
    TextView tv_page;
    Button btn_last;
    Button btn_next;

    private int page = 0;

    private List<WordBean> wordBeans = new ArrayList<>();

    public static void startMe(Context context, ArrayList<WordBean> wordBeans) {
        Intent intent = new Intent(context, WordsDetailActivity.class);
        intent.putExtra("words", wordBeans);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        wordBeans.addAll(getIntent().getParcelableArrayListExtra("words"));
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_words_detail);

        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(v -> finish());
        iv_img = findViewById(R.id.iv_img);
        ll_btn1 = findViewById(R.id.ll_btn1);
        tv_introduce = findViewById(R.id.tv_introduce);
        ll_btn2 = findViewById(R.id.ll_btn2);
        tv_means = findViewById(R.id.tv_means);
        ll_btn3 = findViewById(R.id.ll_btn3);
        tv_short_sentence = findViewById(R.id.tv_short_sentence);
        ll_btn4 = findViewById(R.id.ll_btn4);
        tv_sentence1 = findViewById(R.id.tv_sentence1);
        ll_btn5 = findViewById(R.id.ll_btn5);
        tv_sentence2 = findViewById(R.id.tv_sentence2);
        ll_btn6 = findViewById(R.id.ll_btn6);
        tv_sentence3 = findViewById(R.id.tv_sentence3);
        tv_page = findViewById(R.id.tv_page);
        btn_last = findViewById(R.id.btn_last);
        btn_next = findViewById(R.id.btn_next);

        updatePage();

        btn_next.setOnClickListener(v -> {
            if (page < wordBeans.size()) {
                page++;
            }
            updatePage();
        });
        btn_last.setOnClickListener(v -> {
            if (page != 0) {
                page--;
                updatePage();
            }else {
                Toast.makeText(this, "到头啦~", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void updatePage() {
        if (page >= wordBeans.size()) {
            Toast.makeText(this, "恭喜您已经完成学习~", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        btn_next.setText(page >= wordBeans.size() - 1 ? "完成" : "下一页");
        WordBean wordBean = wordBeans.get(page);
        if (!TextUtils.isEmpty(wordBean.getImgUrl())) {
            if (wordBean.getImgUrl().contains("content://")) {
                Glide.with(this).load(Uri.parse(wordBean.getImgUrl())).into(iv_img);
            } else {
                Glide.with(this).load(wordBean.getImgUrl()).into(iv_img);
            }
        } else {
            Glide.with(this).load(wordBean.getImgLocal()).into(iv_img);
        }

        tv_introduce.setText(wordBean.getIntroduce());
        tv_means.setText(wordBean.getMeans());
        tv_short_sentence.setText(wordBean.getShortSentence());
        // 句子中关键字变色
        tv_sentence1.setText(Html.fromHtml(wordBean.getSentence1().replace(wordBean.getIntroduce(), "<font color='#FF0000'>" + wordBean.getIntroduce() + "</font>")));
        tv_sentence2.setText(Html.fromHtml(wordBean.getSentence2().replace(wordBean.getIntroduce(), "<font color='#FF0000'>" + wordBean.getIntroduce() + "</font>")));
        tv_sentence3.setText(Html.fromHtml(wordBean.getSentence3().replace(wordBean.getIntroduce(), "<font color='#FF0000'>" + wordBean.getIntroduce() + "</font>")));
        tv_page.setText(((page + 1)) + "/" + wordBeans.size());
    }
}